﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcRedovalnica.Models;
using System.Web.Security;


namespace MvcRedovalnica.Controllers
{
    public class accountController : Controller
    {
        //
        // GET: /account/Login

        public ActionResult Login()
        {
            return View();
        }

        //
        // POST: /account/Login

        [HttpPost]
        public ActionResult Login(LoginModel model, string returnUrl) //returnUrl zaradi treh razlicnih grup logirancev???
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.username, model.password))
                {
                    //MigrateShoppingCart(model.UserName);

                    FormsAuthentication.SetAuthCookie(model.username, false); //ce je false, pomeni, da se seja konca, ce zapres browser
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Napacno uporabnisko ime ali geslo!");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        //
        // GET: /account/Logoff

        public ActionResult Logoff()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "home");
        }

    }
}
